<template>
	<view>
		<popUp :showPop="showPop" @close="close">
			<view class="purchase" @touchmove.stop.prevent="moveHandle">
				<image @tap="close(true)" class="colse" src="/static/images/btn_close@2x.png" mode=""></image>
				<view class="header">
					<image :src="thumPic ? thumPic : '/static/images/defaultImg/indexPro.png'" mode="aspectFill"></image>
					<view class="header_details">
						<h5 class="Color333 h-one">{{ goodsInfo.name }}</h5>
						<view v-if="(goodsInfo.integral == 3 || goodsInfo.integral == 4) && goodsInfo.buyType == 2">
							<view class="price">
								{{ integralAmount }}
								<text>积分</text>
								<view v-if="parseFloat(totalPrice) > 0" class="unit">
									<text>+ ¥</text>
									{{ parseFloat(totalPrice) > 0 ? parseFloat(totalPrice) : "" }}
								</view>
							</view>

							<text v-if="parseFloat(goodsInfo.marketPrice) > 0 && goodsInfo.discountFlag" class="fullprice">市场价 ¥{{ Number(goodsInfo.marketPrice) }}</text>
							<text class="" v-if="goodsInfo.integral == 4 && goodsInfo.buyCostPrice">或原价购买¥{{ Number(originalPrice) }}</text>
						</view>
						<view v-else class="price flex" :class="costPrice ? 'juctSpa' : ''">
							<view>
								<text>¥</text>
								{{ Number(totalPrice) }}
							</view>
							<view v-if="costPrice" class="costPrice">原价{{ Number(costPrice) }}</view>
						</view>

						<view class="specifications" v-if="goodsInfo.goodsSkuStockList && goodsInfo.ifSkus == 1">
							{{ selectShopStr ? "已选：" + selectShopStr : "请选择规格" }}
						</view>
						<view class="specifications" v-else>默认规格</view>

						<view class="qp font22" v-if="sgMsg.remark && selectShopStr">
							<image class="image" src="../../score/img/down.png" mode=""></image>
							{{ sgMsg.remark }}
						</view>
					</view>
				</view>
				<view class="afterSale" v-if="!goodsInfo.isScores">{{ goodsInfo.ifAfterSaleStr }}</view>
				<view class="letterleft" v-if="goodsInfo.goodsType != 1">
					<view class="ltitle">{{ !preSale ? "预售" : "现货" }}商品</view>
					<view v-if="!preSale" class="ldicripe xlf-line-two">
						<text v-if="goodsInfo.prepareDeliveryTime">
							{{ Number(goodsInfo.prepareDeliveryTime.split("-")[1]) + "月" }}
							<text v-if="goodsInfo.prepareDeliveryTime.split(',')[goodsInfo.prepareDeliveryTime.split(',').length - 1] == 1">上旬发货</text>
							<text v-if="goodsInfo.prepareDeliveryTime.split(',')[goodsInfo.prepareDeliveryTime.split(',').length - 1] == 2">中旬发货</text>
							<text v-if="goodsInfo.prepareDeliveryTime.split(',')[goodsInfo.prepareDeliveryTime.split(',').length - 1] == 3">下旬发货</text>
						</text>
						<text v-else>{{ goodsInfo.deliveryMsg }} 后发货</text>
					</view>
					<view v-else class="ldicripe xlf-line-two">
						{{ goodsInfo.spotCustDeliveryMsg }}
					</view>
				</view>
				<view class="freeLetter" v-if="goodsInfo.isLettering && goodsInfo.isLettering == 1">
					<view class="letterleft">
						<view class="ltitle">刻字服务</view>
						<view class="ldicripe letterdata">{{ letterdata ? letterdata : "勾选后进入定制" }}</view>
					</view>
					<view v-if="letterdata" class="freeLetter_operation">
						<text @click="cancel">取消</text>
						<text
							@click="
								isUrlTo = false;
								chooseLetter = true;
							">
							修改
						</text>
						<xlf-icon type="more-one" color="#E6E0DA" size="12"></xlf-icon>
					</view>
					<checkbox-group v-else class="letterright">
						<checkbox class="letterright_che" @tap="selectCheck" :checked="checkedLetter" color="#049653" />
					</checkbox-group>
				</view>
				<view class="ge" v-if="goodsInfo.goodsSkuStockList && goodsInfo.ifSkus == 1"></view>
				<view class="specificationsSelect" v-if="goodsInfo.goodsSkuStockList && goodsInfo.ifSkus == 1">
					<scroll-view :scroll-top="0" scroll-y="true" class="scroll-Y" @scroll="seeScrollHeight">
						<view class="speSel_item" v-for="(item, index) in data[specListName]" :key="index">
							<h5 class="Color333">{{ item.name }}</h5>
							<view class="speSel_item_view">
								<view
									:class="[itemTwo.ishow ? '' : 'outOfStock', subIndex[index] == indexTwo ? 'active' : '']"
									v-for="(itemTwo, indexTwo) in item.list"
									:key="indexTwo"
									@click="skuClick(itemTwo, index, indexTwo)">
									{{ itemTwo.name }}
									<text class="outOfStockText" v-if="itemTwo.outOfStock && !itemTwo.ishow">缺货</text>
									<text class="outOfStockText" v-if="itemTwo.isPreSale && itemTwo.ishow">预售</text>
								</view>
							</view>
						</view>
					</scroll-view>
					<!-- -->
					<view class="scrollSelect" v-if="data[specListName].length >= 3 && seeScroll < 50">滑动看更多</view>
				</view>
				<view class="ge"></view>
				<view class="goodsaleinformation" v-if="goodsInfo.numberAdd != 4">
					<view class="keepyears">
						购买数量
						<view v-if="repertoryAmount <= 10">库存 {{ repertoryAmount }}件</view>
						<view v-else>库存充足</view>
					</view>
					<view class="keepyearsbox row">
						<view class="goodsreducebu" @tap="landnumreduce" :class="{ inputdisable: inputValue == 1 }">-</view>
						<view class="nump">
							<input type="number" @input="inputValueClick" v-model="inputValue" @blur="_onBlur" />
						</view>
						<view
							class="goodsaddbu"
							@tap="landnumadd"
							:class="{
								inputdisable: inputValue == (goodsInfo.oneBuyPromotionVO && oneRepertoryAmount < repertoryAmount ? oneRepertoryAmount : repertoryAmount)
							}">
							+
						</view>
					</view>
				</view>
				<view class="purchaseLimitType" v-if="goodsInfo.purchaseLimitType != 0 && goodsInfo.numberAdd != 4 && !goodsInfo.oneBuyPromotionVO">
					{{ purchaseLimit.day }}限购{{ purchaseLimit.num }}件
					<text v-if="goodsInfo.alreadyBuyNum">（已购{{ goodsInfo.alreadyBuyNum }}件）</text>
				</view>
				<view class="purchaseLimitType onePurchaseLimitType" v-if="goodsInfo.oneBuyPromotionVO">限购{{ oneRepertoryAmount }}件</view>
				<view v-if="goodsInfo.numberAdd == 3 && origin == 1" class="buttonTwo row-between">
					<view
						v-if="!goodsInfo.oneBuyPromotionVO"
						class="row-center"
						@click="
							numThress = 1;
							btnthrottle();
						">
						加入购物车
					</view>
					<view
						class="row-center"
						:class="goodsInfo.oneBuyPromotionVO ? 'buttonTwo_qone' : ''"
						@click="
							numThress = 2;
							btnthrottle();
						">
						立即购买
					</view>
				</view>
				<view v-else class="button" @click="btnthrottle">确定</view>
			</view>
		</popUp>
		<!-- 免费刻字 -->
		<lettering
			v-if="goodsInfo.isLettering && goodsInfo.isLettering == 1"
			:isUrlTo="isUrlTo"
			:cartMessage="goodsInfo"
			:show="chooseLetter"
			@closeLettering="closeLettering"
			@lettersure="lettersure"></lettering>

		<screen
			v-if="openModel"
			v-model="openModel"
			@left="openModel = false"
			@right="openModel = false"
			color="#33C2B4"
			title="当前规格不可购买"
			:tips="'此商品需购买了(' + sgMsg.firstPurchaseProperties + ')规格后才可继续购买其他规格'"
			:type="2"
			buttonLeft="取消"
			buttonRight="确认"></screen>
		<!-- 积分不足的弹框 -->
		<view class="tip-box" v-if="integralShow">
			<view class="box text-center">
				<image class="light" src="/static/images/score/light.png" mode=""></image>
				<image class="cry" src="/static/images/score/cry.png" mode=""></image>
				<view class="font32 m-t-60">
					<view class="">积分不足</view>
					<view class="font32">无法兑换</view>
				</view>
				<view class="row-around m-t-40 font32 bt" style="border-color: #eee">
					<text @click="navTo('/pages/score/earn-score/earn-score')" class="fx p-t-30 p-b-30 c-green br" style="border-color: #eee">做任务赚积分</text>
					<text @click="integralShow = false" class="fx p-t-30 p-b-30">好的</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import popUp from "../../cleanser/components/popup-layer.vue";
import lettering from "@/pages/cleanser/components/lettering.vue";
import screen from "@/pages/check/component/screen.vue";
import { fastLogin } from "@/common/util/otherLogin.js";
import { differenceTime } from "@/common/util/date.js";
import { userLevelsSwitch, fnThrottle } from "@/common/util/filters";
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
import { purchaseCheck, getGoodsSaleTime, goodsDetailPrice, refereeRecord } from "@/api/mall/goods.js";
import { oneBuyPromotionCheckOneBuyPromotion } from "@/api/oneYuan/index.js";
export default {
	data() {
		return {
			goodsInfo: {}, //商品详情数据
			inputValue: 1, //购买数量
			repertoryAmount: 200, // 库存
			oneRepertoryAmount: 1, //一元购可购买数量
			checkedLetter: true, //是否刻字
			purchaseLimit: {}, //限购信息
			chooseLetter: false, //刻字弹框
			letterdata: "", //刻字内容
			stockNum: 0, //已选规格的库存

			data: {}, //数据集
			selectArr: [], // 存放被选中的值
			subIndex: [], // 是否选中 因为不确定是多规格还是单规格，所以这里定义数组来判断
			selectShop: {}, // 存放最后选中的商品
			selectNum: 1, // 最小购买数量 选中数量
			outFoStock: false, // 是否全部sku都缺货
			specListName: "spec_list", //需要筛选的参数
			skuListName: "sku_list", //需要筛选的参数
			complete: false, // 组件是否加载完成
			initKey: true, // 是否已初始化
			stockName: "stock", //库存的字段名
			skuArrName: "sku_name_arr", //sku组合路径的字段名
			maxBuyNum: 200, // 最大购买数量
			minBuyNum: 1, //最小购买数量
			selectShopStr: "", //已选规格
			totalPrice: "", //价格
			integralAmount: "", //积分
			thumPic: "", //规格图片
			flay: true, //是否可以购买
			spotDeliveryMsg: {
				//几点发货
				str: "",
				num: 0
			},
			isUrlTo: true, //判断是否可以刻字完成直接进行下一步
			preSale: true, //true  现货  false  预售
			originalPrice: null, //原价
			sgMsg: {},
			openModel: false,
			integralShow: false, //积分不足的弹框
			costPrice: "", //原价

			numThress: 1, //在numberAdd==3的情况下，点击了哪个
			seeScroll: 0 //规格滚动距离
		};
	},
	props: {
		// 是否使用缓存
		useCache: {
			Type: Boolean,
			default: true
		},
		showPop: {
			Type: Boolean,
			default: false
		},
		cartMessage: {
			Type: Object,
			default: {}
		},
		origin: {
			//1  普通商城  2  农场商城  0 积分商城
			Type: Number,
			default: 0
		},
		upgradeConfigId:{
			Type: Number | String,
			default: 0
		}
	},
	// 计算属性
	computed: {
		...mapGetters(["userInfo", "hasLogin"])
	},
	components: {
		popUp,
		lettering,
		screen
	},
	watch: {
		showPop(m) {
			if (m) {
				this.open();
			}
			this.chooseLetter = false;
		}
	},
	onHide() {
		uni.hideToast();
	},
	methods: {
		...mapMutations(["SET_SHOWPOP"]),
		...mapActions(["getAddCard", "getOrderList", "getGartList"]),
		open() {
			this.flay = false;
			this.goodsInfo = this.cartMessage;
			if (this.goodsInfo) {
				// if(JSON.stringify(this.data) != "{}") return
				if (this.goodsInfo.ifSkus == 1) {
					var lists = [],
						data = {};
					data.sku_list = this.goodsInfo.goodsSkuStockList;
					data.sku_list.forEach((item) => {
						item.sku_name_arr = [];
						var propertiesList = JSON.parse(item.properties);
						propertiesList.forEach((i) => {
							item.sku_name_arr.push(i.value);
						});
					});
					this.goodsInfo.goodsSkuStockList.forEach((item) => {
						item.stock = item.stocks + item.prepareStocks;
						item.propertiesList = JSON.parse(item.properties);
						item.propertiesList.forEach((i, index) => {
							lists.push(i);
						});
					});
					let newobj = {};
					lists = lists.reduce((preVal, curVal) => {
						newobj[curVal.value] ? "" : (newobj[curVal.value] = preVal.push(curVal));
						return preVal;
					}, []);
					let arrList = [];
					lists.forEach((item, index) => {
						if (arrList.length <= 0) {
							arrList.push({
								name: item.key,
								list: [
									{
										name: item.value
									}
								]
							});
						} else {
							let res = arrList.find((citem, i) => {
								if (citem.name == item.key || citem.name == item.key) {
									arrList[i].list.push({
										name: item.value
									});
									return true;
								}
							});
							if (!res) {
								arrList.push({
									name: item.key,
									list: [
										{
											name: item.value
										}
									]
								});
							}
						}
					});
					data.spec_list = arrList;
					this.updateGoodsInfo(data); // 更新商品信息(库存、名称、图片)
					this.$emit("open", true);
					let item = this.goodsInfo.goodsSkuStockList.find((item) => {
						return item.stocks + item.prepareStocks > 0;
					});
					this.thumPic = this.goodsInfo.oneBuyPromotionVO ? this.goodsInfo.oneBuyPromotionVO.pic : item.pic;
				} else {
					this.selectShop = this.goodsInfo.goodsSkuStockList[0];
					this.preSale = this.goodsInfo.goodsSkuStockList[0].stocks != 0 ? true : false;
					this.repertoryAmount = this.goodsInfo.goodsSkuStockList[0].stocks + this.goodsInfo.goodsSkuStockList[0].prepareStocks;
					this.getGoodsDetailPrice(this.goodsInfo.goodsSkuStockList[0].id);
				}
				if (this.goodsInfo.oneBuyPromotionVO) {
					this.oneRepertoryAmount = this.cartMessage.oneBuyPromotionVO.allNum - this.cartMessage.oneBuyPromotionVO.alreadyNum;
				}
			}
			this.letterdata = this.goodsInfo.letteringMsg;
			if (this.goodsInfo.numberAdd == 4 || this.upgradeConfigId) {
				this.inputValue = this.goodsInfo.amount;
			}
			if (this.goodsInfo.spotDeliveryMsg) {
				let spotDeliveryMsg = this.goodsInfo.spotDeliveryMsg.split(",");
				this.spotDeliveryMsg.str = spotDeliveryMsg[0];
				if (spotDeliveryMsg.length == 2) {
					this.spotDeliveryMsg.num = spotDeliveryMsg[1];
				}
			}
			this.purchaseLimit = {};
			//判断限购显示信息 qc
			if (this.goodsInfo.purchaseLimitType != 0 && !this.goodsInfo.oneBuyPromotionVO) {
				if (this.goodsInfo.purchaseLimitType == 1) {
					var arr = this.goodsInfo.purchaseLimitRules.split(",");
				}
				var num = this.goodsInfo.purchaseLimitType == "1" ? arr[1] : this.goodsInfo.purchaseLimitRules;
				if (this.goodsInfo.purchaseLimitType == 1) {
					var day = arr[0] < 7 ? "每天" : arr[0] == 7 ? "每周" : arr[0] <= 31 ? "每月" : "每年";
				} else {
					var day = "永久";
				}
				this.purchaseLimit.num = num;
				this.purchaseLimit.day = day;
				if (num < this.inputValue) {
					this.inputValue = num;
				}
			}
			// 自定义说明
			if (!this.goodsInfo.spotCustDeliveryMsg || this.goodsInfo.spotCustDeliveryMsg == "") {
				this.goodsInfo.spotCustDeliveryMsg = `${this.goodsInfo.spotDeliveryMsg ? this.spotDeliveryMsg.str : 15}点前下单当天发货，${
					this.goodsInfo.spotDeliveryMsg ? this.spotDeliveryMsg.str : 15
				}点后的订单次日发货${this.goodsInfo.spotDeliveryMsg && this.spotDeliveryMsg.num == 1 ? ",周末及法定节假日顺延" : ""}`;
			}
		},
		// sku初始化
		init(notAutoClick) {
			let that = this;
			// 清空之前的数据
			that.selectArr = [];
			that.subIndex = [];
			that.selectShop = {};
			that.selectNum = 1;
			that.outFoStock = false;
			that.shopItemInfo = {};
			let specListName = that.specListName;
			// 处理显示数据
			that.data[specListName].map((item) => {
				that.selectArr.push("");
				that.subIndex.push(-1);
			});
			that.checkItem(); // 计算sku里面规格形成路径
			that.checkInpath(-1); // 传-1是为了不跳过循环
			// if (!notAutoClick) that.autoClickSku(); // 自动选择sku策略
		},
		// 更新商品信息(库存、名称、图片)
		updateGoodsInfo(data) {
			let that = this;
			let { skuListName } = that;
			that.data = data;
			that.initKey = true;
			if (that.initKey) {
				that.initKey = false;
				that.init();
			}
			// 更新选中sku的库存信息
			let select_sku_info = that.getListItem(that.data[skuListName], that.skuIdName, that.selectShop[that.skuIdName]);
			Object.assign(that.selectShop, select_sku_info);
			that.defaultSelectSku();
			that.complete = true;
		},
		defaultSelectSku() {
			let that = this;
			let { defaultSelect } = that;
			if (defaultSelect && defaultSelect.sku && defaultSelect.sku.length > 0) {
				that.selectSku(defaultSelect);
			}
		},
		selectSku(obj = {}) {
			let that = this;
			let { sku: skuArr, num: selectNum } = obj;
			let specListArr = that.data[that.specListName];
			if (skuArr && specListArr.length === skuArr.length) {
				// 先清空
				let skuClickArr = [];
				let clickKey = true;
				for (let index = 0; index < skuArr.length; index++) {
					let skuName = skuArr[index];
					let specList = specListArr[index].list;
					let index1 = index;
					let index2 = that.getListIndex(specList, "name", skuName);
					if (index2 == -1) {
						clickKey = false;
						break;
					}
					skuClickArr.push({
						spec: specList[index2],
						index1: index1,
						index2: index2
					});
				}
				if (clickKey) {
					that.init(true);
					skuClickArr.map((item) => {
						that.skuClick(item.spec, item.index1, item.index2, true);
					});
				}
			}
			if (selectNum > 0) that.selectNum = selectNum;
		},
		priceFilter(n = 0) {
			let that = this;
			if (typeof n == "string") {
				n = parseFloat(n);
			}
			if (that.amountType === 0) {
				return n.toFixed(2);
			} else {
				return (n / 100).toFixed(2);
			}
		},
		// 计算sku里面规格形成路径
		checkItem() {
			let that = this;
			let { stockName } = that;

			let skuListName = that.skuListName;
			// 去除库存小于等于0的商品sku
			let originalSkuList = that.data[skuListName];
			let skuList = [];
			let stockNum = 0;
			originalSkuList.map((skuItem, index) => {
				// skuList.push(skuItem);
				// stockNum += skuItem[stockName];
				if (skuItem[stockName] >= 0) {
					skuList.push(skuItem);
					stockNum += skuItem[stockName];
				}
			});
			if (stockNum <= 0) {
				that.outFoStock = true;
			}
			// 计算有多小种可选路径
			let result = skuList.reduce(
				(arrs, items) => {
					return arrs.concat(
						items[that.skuArrName].reduce(
							(arr, item) => {
								return arr.concat(
									arr.map((item2) => {
										// 利用对象属性的唯一性实现二维数组去重
										if (!that.shopItemInfo.hasOwnProperty(that.getArrayToSting([...item2, item]))) {
											that.shopItemInfo[that.getArrayToSting([...item2, item])] = items;
										}
										return [...item2, item];
									})
								);
							},
							[[]]
						)
					);
				},
				[[]]
			);
		},
		//拼接
		getArrayToSting(arr) {
			let str = "";
			arr.map((item, index) => {
				item = item.replace(/\./g, "。");
				if (index == 0) {
					str += item;
				} else {
					str += "," + item;
				}
			});
			return str;
		},
		// 自动选择sku前提是只有一组sku,默认自动选择最前面的有库存的sku
		autoClickSku() {
			let that = this;
			let skuList = that.data[that.skuListName];
			let specListArr = that.data[that.specListName];
			if (specListArr.length == 1) {
				let specList = specListArr[0].list;
				for (let i = 0; i < specList.length; i++) {
					let sku = that.getListItem(skuList, that.skuArrName, [specList[i].name]);
					if (sku) {
						that.skuClick(specList[i], 0, i);
						break;
					}
				}
			}
		},
		// 检查路径 循环所有属性判断哪些属性可选  当前选中的兄弟节点和已选中属性不需要循环
		checkInpath(clickIndex) {
			let that = this;
			let specListName = that.specListName;
			//循环所有属性判断哪些属性可选
			//当前选中的兄弟节点和已选中属性不需要循环
			let specList = that.data[specListName];
			for (let i = 0, len = specList.length; i < len; i++) {
				if (i == clickIndex) {
					continue;
				}
				let len2 = specList[i].list.length;
				for (let j = 0; j < len2; j++) {
					// if (that.subIndex[i] != -1 && j == that.subIndex[i]) {
					// 	continue;
					// }
					let choosed_copy = [...that.selectArr];
					that.$set(choosed_copy, i, specList[i].list[j].name);
					let choosed_copy2 = choosed_copy.filter((item) => item !== "" && typeof item !== "undefined");

					let key = that.getArrayToSting(choosed_copy2);
					if (that.shopItemInfo.hasOwnProperty(key) && that.shopItemInfo[key].stock != 0) {
						specList[i].list[j].ishow = true;
						// 判断预售
						if (that.shopItemInfo[key].stocks == 0 && that.shopItemInfo[key].stocksprepareStocks != 0) {
							specList[i].list[j].isPreSale = true;
						} else {
							specList[i].list[j].isPreSale = false;
						}
					} else {
						specList[i].list[j].ishow = false;
						// 判断是否没有货了 缺货
						if (that.shopItemInfo.hasOwnProperty(key) && Number(that.shopItemInfo[key].stock) == 0) {
							specList[i].list[j].outOfStock = true;
						} else {
							specList[i].list[j].outOfStock = false;
						}
					}
				}
			}
			that.$set(that.data, specListName, specList);
		},
		// 获取对象数组中的某一个item,根据指定的键值
		getListItem(list, key, value) {
			let that = this;
			let item;
			for (let i in list) {
				if (typeof value == "object") {
					if (JSON.stringify(list[i][key]) === JSON.stringify(value)) {
						item = list[i];
						break;
					}
				} else {
					if (list[i][key] === value) {
						item = list[i];
						break;
					}
				}
			}
			return item;
		},
		getListIndex(list, key, value) {
			let that = this;
			let index = -1;
			for (let i = 0; i < list.length; i++) {
				if (list[i][key] === value) {
					index = i;
					break;
				}
			}
			return index;
		},
		// 检测是否已经选完sku
		checkSelectShop() {
			let that = this;
			// 如果全部选完
			if (that.selectArr.every((item) => item != "")) {
				that.selectShopStr = "";
				that.selectShop = that.shopItemInfo[that.getArrayToSting(that.selectArr)];
				that.repertoryAmount = that.selectShop.stock ? that.selectShop.stock : 200;
				that.preSale = that.selectShop.stocks > 0 ? true : false;
				if (that.inputValue > that.repertoryAmount) {
					that.inputValue = that.repertoryAmount;
				}
				let stocks = that.selectShop[that.stockName];
				if (typeof stocks !== "undefined" && that.selectNum > stocks) {
					that.selectNum = stocks;
				}
				if (that.selectNum > that.maxBuyNum) {
					that.selectNum = that.maxBuyNum;
				}
				if (that.selectNum < that.minBuyNum) {
					that.selectNum = that.minBuyNum;
				}
				if (that.selectedInit) {
					that.selectNum = that.minBuyNum || 1;
				}
			} else {
				that.selectShop = {};
			}
			if (that.selectShop.sku_name_arr) {
				that.selectShop.sku_name_arr.forEach((item) => {
					that.selectShopStr += item + " ; ";
				});
				that.thumPic = that.goodsInfo.oneBuyPromotionVO ? that.goodsInfo.oneBuyPromotionVO.pic : that.selectShop.pic;
				that.selectShopStr = that.selectShopStr.substr(0, that.selectShopStr.length - 2);
				that.selectShop.selectShopStr = that.selectShopStr;
				that.getGoodsDetailPrice();
			} else {
				that.selectShopStr = "";
				that.selectShop.selectShopStr = "";
			}
		},
		// sku按钮的点击事件
		skuClick(value, index1, index2, offer) {
			let that = this;
			if (value.ishow || offer) {
				if (that.selectArr[index1] != value.name) {
					that.$set(that.selectArr, index1, value.name);
					that.$set(that.subIndex, index1, index2);
				} else {
					that.$set(that.selectArr, index1, "");
					that.$set(that.subIndex, index1, -1);
				}
				that.checkInpath(index1);
				// 如果全部选完
				that.checkSelectShop();
			}
		},
		//获取商品价格
		getGoodsDetailPrice(skuCode = this.selectShop.id) {
			goodsDetailPrice({
				goodsId: this.goodsInfo.id,
				skuCode: skuCode
			}).then((res) => {
				this.thumPic = this.selectShop.pic;
				if (res.data) {
					this.sgMsg = res.data;
					this.sgMsg.remark = res.data.remark;
					// if (this.sgMsg.firstCanBuyFlag == 0) {
					// 	this.openModel = true
					// }else{
					// 	this.openModel = false;
					// }
					this.totalPrice = this.goodsInfo.oneBuyPromotionVO ? this.goodsInfo.oneBuyPromotionVO.price : res.data.price;
					if (this.goodsInfo.integral) {
						if (this.goodsInfo.integral == 3) {
							this.totalPrice = res.data.exchangePrice;
						}
						if (this.goodsInfo.integral == 4) {
							if (this.goodsInfo.buyType == 2) {
								this.totalPrice = res.data.exchangePrice;
							}
							this.originalPrice = res.data.price;
						}
						this.integralAmount = res.data.integralAmount;
					}
					if (res.data.costPrice) {
						this.costPrice = res.data.costPrice;
					}
					this.flay = true;
				}
			});
		},
		// 关闭
		close(value) {
			if (value) {
				this.$emit("selectShop", this.selectShop, true);
			} else {
				this.$emit("selectShop", this.selectShop);
			}
		},
		// 是否选择刻字
		selectCheck() {
			this.checkedLetter = !this.checkedLetter;
		},
		//关闭刻字弹框
		closeLettering() {
			this.chooseLetter = false;
		},
		// 取消刻字
		cancel() {
			let that = this;
			uni.showModal({
				title: "是否取消刻字",
				content: "注：取消刻字后可以重新定制",
				success: function (res) {
					if (res.confirm) {
						that.letterdata = "";
						that.goodsInfo.letteringMsg = "";
						that.checkedLetter = false;
					} else if (res.cancel) {
					}
				}
			});
		},
		// 接受刻字数据
		lettersure(data, isUrlTo) {
			this.isUrlTo = isUrlTo;
			this.letterdata = data.letteringMsg;
			this.chooseLetter = false;
			if (this.letterdata && this.isUrlTo) {
				this.btnthrottle();
			}
			this.isUrlTo = true;
		},
		//购买数量减
		landnumreduce() {
			if (this.inputValue > 1) {
				this.inputValue--;
			} else {
				this.$msg.toast("最低购买一件");
				return false;
			}
		},
		//购买数量加
		landnumadd() {
			let repertoryAmount = this.goodsInfo.oneBuyPromotionVO && this.oneRepertoryAmount < this.repertoryAmount ? this.oneRepertoryAmount : this.repertoryAmount;
			if (this.inputValue < repertoryAmount) {
				this.inputValue++;
			} else {
				this.$msg.toast("最多只能购买" + repertoryAmount + "件");
				return false;
			}
			//如果超出限购数量 提示  数量为限购数量
			this.goodsInfo.alreadyBuyNum = this.goodsInfo.oneBuyPromotionVO
				? this.goodsInfo.oneBuyPromotionVO.allNum - this.goodsInfo.oneBuyPromotionVO.alreadyNum
				: this.goodsInfo.alreadyBuyNum
				? this.goodsInfo.alreadyBuyNum
				: 0;
			if (!this.goodsInfo.oneBuyPromotionVO && this.goodsInfo.purchaseLimitType != 0 && this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum < this.inputValue) {
				this.inputValue = this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum <= 0 ? 1 : this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum;
				this.$msg.toast(`最多可购买${this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum < 0 ? 0 : this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum}件`);
			}
			if (this.goodsInfo.integral == 3 && this.integralAmount * this.inputValue > this.goodsInfo.scores) {
				this.inputValue = Math.floor(this.goodsInfo.scores / this.integralAmount);
				this.$msg.toast(`积分不足，最多可兑换${this.inputValue}件`);
			}
		},
		// 积分不足 跳转
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		//购买数量  填写
		inputValueClick(e) {
			// 一元购禁止更改数据 1
			if (this.goodsInfo.oneBuyPromotionVO) {
				return;
			}
			this.$nextTick(() => {
				let repertoryAmount = this.goodsInfo.oneBuyPromotionVO && this.oneRepertoryAmount < this.repertoryAmount ? this.oneRepertoryAmount : this.repertoryAmount;
				setTimeout((_) => {
					// 校验输入的数量是不是正整数
					if (this.inputValue === "" && this.inputValue != 0) {
						this.$msg.toast("件数只能为正整数哦~");
						this.inputValue = 1;
					}
					if (String(this.inputValue).indexOf(".") > -1) {
						this.$msg.toast("件数只能为整数");
						this.inputValue = parseInt(this.inputValue);
					}
					//如果 输入值大于库存,输入值就等于库存
					if (this.inputValue > repertoryAmount) {
						this.inputValue = repertoryAmount;
						this.$msg.toast("最多只能购买" + repertoryAmount + "件");
					}
					// 针对app里面uni的input赋值不成功的处理111  e.detail.value> this.maxuserbagnum
					if (e.detail.value > repertoryAmount) {
						this.inputValue = repertoryAmount;
						this.$msg.toast("最多只能购买" + repertoryAmount + "件");
					}
					if (this.goodsInfo.integral == 3) {
						if (this.integralAmount * this.inputValue > this.goodsInfo.scores) {
							this.inputValue = Math.floor(this.goodsInfo.scores / this.integralAmount);
							this.$msg.toast(`最多可购买${this.inputValue}件`);
						}
					}
					//如果超出限购数量 提示  数量为限购数量
					if (this.purchaseLimit && this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum < this.inputValue) {
						var num = this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum < 0 ? 0 : this.purchaseLimit.num - this.goodsInfo.alreadyBuyNum;
						if (num == 0) {
							this.$msg.toast(`已达到限购件数，不能购买`);
						} else {
							this.$msg.toast(`最多可购买${num}件`);
						}

						this.inputValue = num;
					}
				}, 0);
			});
		},

		// 输入框获取焦点
		_onBlur(event) {
			let value = event.detail.value;
			if (!value) {
				this.inputValue = 1;
				return;
			}
			value = +value;
			let num = this.selectShop.stock;
			if (this.goodsInfo.oneBuyPromotionVO && this.oneRepertoryAmount < this.repertoryAmount) {
				num = this.oneRepertoryAmount;
			}
			if (value > num) {
				value = num;
			} else if (value < 1) {
				value = 1;
			}
			this.inputValue = value;
		},
		//节流函数
		btnthrottle() {
			if (!this.$store.getters.hasLogin) {
				this.loginModel();
				return;
			}
			// numberAdd  1  加入购物车  2  立即购买  3  选择规格颜色  return信息   4  购物车进入 回显 替换   5 优惠券商品购买
			if (this.goodsInfo.numberAdd == 3 && this.origin != 1) {
				// 购买商品积分不足
				if (this.goodsInfo.scores && this.integralAmount > this.goodsInfo.scores) {
					this.integralShow = true;
				} else {
					this.close();
				}
				return;
			}
			if (this.sgMsg && this.sgMsg.firstCanBuyFlag == 0) {
				this.openModel = true;
				return;
			}
			// checkedLetter  是否勾选可以刻字   isLettering 商品是否容许刻字    letterdata  刻字内容
			if (!this.checkedLetter || !this.goodsInfo.isLettering || this.letterdata) {
				let that = this;
				if (!this.selectShop.selectShopStr && this.goodsInfo.ifSkus == 1) {
					this.$msg.toast("请选择规格");
					return;
				}
				if (this.goodsInfo.isLettering && !this.checkedLetter) {
					this.letterdata = "";
					this.goodsInfo.letteringMsg = "";
				}
				if (
					this.goodsInfo.numberAdd == 1 ||
					this.goodsInfo.numberAdd == 4 ||
					(this.goodsInfo.numberAdd == 3 && this.numThress == 1) ||
					this.goodsInfo.numberAdd == 5
				) {
					this.addShopCart(); //加入购物车
				} else {
					this.buy();
				}
			} else {
				if (this.goodsInfo.numberAdd == 3) {
					this.isUrlTo = false;
				}
				this.chooseLetter = true;
			}
		},
		//推荐人处理
		async dealReferee() {
			// 接口已作废
			// if(this.$store.state.user.rebrandId&&this.cartMessage.brandsId == 10035 && !this.$store.state.user.isRebrand ){
			// 	const res = await refereeRecord(this.cartMessage.brandsId,this.$store.state.user.userInfo.id,this.$store.state.user.rebrandId)
			// 	this.$store.commit('SET_ISREBRAND',res.data)
			// }
		},
		// 加入购物车
		async addShopCart() {
			if (this.flay) {
				this.flay = false;
				let goodsInfo = this.goodsInfo;
				goodsInfo.amount = this.inputValue;
				goodsInfo.prepareStocks = this.selectShop.prepareStocks;
				goodsInfo.stocks = this.selectShop.stocks;
				goodsInfo.removeSkuCode = goodsInfo.skuCode;
				goodsInfo.skuCode = this.goodsInfo.ifSkus == 1 ? this.selectShop.id : this.goodsInfo.goodsSkuStockList[0].id;
				goodsInfo.goodsSkuStockList.forEach((item) => {
					if (item.id == goodsInfo.skuCode) {
						goodsInfo.skuProperties = item.properties;
					}
				});
				if (this.letterdata) {
					goodsInfo.letteringMsg = this.letterdata;
				}
				const params = {
					addType: 1,
					arr: [goodsInfo],
					ifRadio: false,
					isWaiting: true,
					arrType: 1
				};
				if (this.goodsInfo.numberAdd == 4) {
					params.addType = 2;
					params.isSelect = true;
				}
				if (this.goodsInfo.standard == 6) {
					params.addType = 6;
				}

				const res = await this.getAddCard(params);
				this.dealReferee();

				if (res.code == 0) {
					if (this.goodsInfo.numberAdd == 4) {
						// this.$msg.toast('修改成功！');
					} else {
						this.$msg.toast("加入购物车成功！");
					}
					if (this.goodsInfo.numberAdd == 5) {
						this.getGartList();
					}

					this.close();
				}
				this.flay = true;
			}
		},
		// 立即购买
		async buy() {
			if (this.inputValue > 0) {
				if (this.flay) {
					this.flay = false;
					if (this.goodsInfo.status == 2) {
						//在售状态 0-即将开售，1-在售 2-下架
						//已加入购物车的商品下架后进入商品详情进行购买
						this.$msg.toast("该商品已下架,看看其他的吧!");
						this.flay = true;
					} else if (this.goodsInfo.goodsStatus == 3 || (this.goodsInfo.goodsStatus == 2 && this.goodsInfo.saleType == 1)) {
						//缺货
						this.$msg.toast("该商品已售罄,看看其他的吧!");
						this.flay = true;
					} else if (this.sgMsg && this.sgMsg.firstCanBuyFlag == 0) {
						this.openModel = true;
					} else {
						if (this.goodsInfo.oneBuyPromotionVO) {
							let res = await oneBuyPromotionCheckOneBuyPromotion(this.goodsInfo.id);
							//一元购处理
							if (res.data.identityCheck != "0" || res.data.allNum <= res.data.alreadyNum) {
								this.selectShop.isOne = true;
								this.$emit("selectShop", this.selectShop);
								return;
							}
						}
						let goodsInfo = this.goodsInfo;
						goodsInfo.amount = this.inputValue;
						goodsInfo.count = goodsInfo.amount;
						goodsInfo.goodsId = goodsInfo.id;
						goodsInfo.refereeId = this.$store.state.user.rebrandId;

						goodsInfo.prepareStocks = this.selectShop.prepareStocks;
						goodsInfo.stocks = this.selectShop.stocks;
						goodsInfo.price = this.totalPrice;
						goodsInfo.thumPic = this.thumPic;
						goodsInfo.deliverWeight = this.selectShop.weight;
						goodsInfo.deliverVolume = this.selectShop.volume; //发货体积
						goodsInfo.skuCode = this.goodsInfo.ifSkus == 1 ? this.selectShop.id : this.goodsInfo.goodsSkuStockList[0].id; //发货重量
						goodsInfo.goodsSkuStockList.forEach((item) => {
							if (item.id == goodsInfo.skuCode) {
								goodsInfo.skuProperties = item.properties;
							}
						});
						if (this.goodsInfo.integral) {
							// 1  现金购买   2  原价购买   3  积分兑换
							goodsInfo.scoreType = "score";
							goodsInfo.buyCostPrice = 1;
							// 原价购买
							if (this.goodsInfo.integral == 1 || this.goodsInfo.integral == 2) {
								goodsInfo.price = this.totalPrice;
							}
							//   积分兑换
							if (this.goodsInfo.integral == 3) {
								goodsInfo.buyCostPrice = 0;
								goodsInfo.integralAmount = 0;
								goodsInfo.exchangePrice = this.totalPrice;
								goodsInfo.scoreAmount = this.integralAmount;
							}
						}
						if (this.letterdata) {
							goodsInfo.letteringMsg = this.letterdata;
						}
						let res = await this.getOrderList([goodsInfo]);
						this.dealReferee();
						if (res && !res.code) {
							console.log(goodsInfo);

							// score  积分商品  productType  product  普通商品  oneYuan  一元购    farm  农场商品
							let url = `/pages/cart/confirmOrder?productType=${
								this.goodsInfo.integral ? "score" : this.goodsInfo.oneBuyPromotionVO ? "oneYuan" : this.origin == 2 ? "farm" : "product"
							}&prohibit=true`;
							if (goodsInfo.hideTemporarySave) {
								url = `/pages/cart/confirmOrder?productType=product&hideTemporarySave=true&remarks=${goodsInfo.remarks}`;
							}
							this.close(); //隐藏弹框
							uni.navigateTo({
								url: url
							});
							this.inputValue = 1; //恢复数量输入框值为1
						} else {
							this.selectShop = null;
							this.close(); //隐藏弹框
						}
						// this.flay = true;
					}
				}
			} else {
				this.$msg.toast("购买数量不能为空");
			}
		},
		// 未登录提示
		loginModel() {
			//未登录提示
			this.$msg
				.showModal({
					content: "您还未登录，请先登录!",
					confirmText: "去登录"
				})
				.then((res) => {
					if (res.confirm) {
						// #ifndef H5
						fastLogin();
						// #endif

						// #ifdef H5
						if (this.$store.getters.refereeId) {
							this.navTo(`/pages/register/H5-teamInvited?refereeId=${this.$store.getters.refereeId}`, false); //跳转至登录页面
						} else {
							fastLogin();
						}
						// #endif
					}
				});
		},
		//规格滚动高度
		seeScrollHeight(e) {
			this.seeScroll = e.detail.scrollTop;
		},
		moveHandle() {}
	}
};
</script>

<style lang="scss" scoped>
.qp {
	background-color: rgba(0, 0, 0, 0.35);
	padding: 10rpx;
	border-radius: 10rpx;
	margin-top: 25rpx;
	color: #fff;
	position: relative;
	word-break: break-all;
	.image {
		position: absolute;
		left: 20%;
		top: -21rpx;
		width: 35rpx;
		height: 25rpx;
		transform: rotate(180deg);
		opacity: 0.35;
	}
}

.purchase {
	width: 100%;
	box-sizing: border-box;
	background-color: #ffffff;
	padding: 32rpx 32rpx calc(32rpx + env(safe-area-inset-bottom));
	overflow: hidden;
	max-height: 100vh;
	border-radius: 18px 18px 0 0;
	box-sizing: border-box;
	position: relative;
}

.colse {
	width: 40rpx;
	height: 40rpx;
	z-index: 2;
	position: absolute;
	border-radius: 50%;
	right: 40rpx;
	top: 32rpx;
	background-color: #f2f2f2;
}

//头部
.header {
	display: flex;
	align-items: flex-end;
	margin-bottom: 48rpx;

	image {
		width: 200rpx;
		height: 200rpx;
		border-radius: 24rpx;
		margin-right: 24rpx;
	}

	.header_details {
		width: calc(100% - 284rpx);
	}

	h5 {
		font-size: 26rpx;
	}

	.price {
		margin: 8rpx 0;
		color: #fd5454;
		font-size: 32rpx;
		display: flex;
		font-weight: 700;
		align-items: center;

		text {
			font-size: 24rpx;
			margin: 0 4rpx;
		}

		.fontSize12 {
			font-size: 24rpx;
		}
	}
	.juctSpa {
		justify-content: space-between;
	}
	.fullprice {
		font-size: 24upx;
		font-weight: bold;
		color: #bfb5ac;
		display: block;
		text-decoration: line-through;
		// margin-left: 60upx;
		margin-bottom: -2upx;
	}

	.specifications {
		color: #7c807f;
		font-size: 26rpx;
	}
}
// 售后
.afterSale {
	color: #1b2120;
	font-size: 24rpx;
	margin-bottom: 32rpx;
}
//刻字  预售  开始
.freeLetter {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 40rpx;

	.letterleft {
		margin: 0;
	}

	.letterright {
		width: 32rpx;
		height: 32rpx;
		position: relative;

		.letterright_che {
			transform: scale(0.8);
			position: absolute;
			left: -8rpx;
			top: -6rpx;
		}
	}

	.freeLetter_operation {
		display: flex;
		align-items: center;

		text {
			font-size: 24rpx;
			color: #1b2120;
			margin-right: 10rpx;

			&:first-child {
				color: #cacccc;
			}
		}
	}
}

.letterleft {
	display: flex;
	font-size: 24rpx;
	align-items: center;
	margin-bottom: 32rpx;

	.ltitle {
		color: #1b2120;
		margin-right: 16rpx;
	}

	.ldicripe {
		width: 500rpx;
		color: #aaaaaa;
	}

	.letterdata {
		width: 300rpx;
	}
}

//选择规格
.specificationsSelect {
	height: 330rpx;
	overflow-y: scroll;
	padding: 20rpx 0 8rpx;
	box-sizing: border-box;
	position: relative;
	.scrollSelect {
		color: #fff;
		position: absolute;
		right: 0;
		bottom: 0;
		padding: 6rpx 10rpx;
		font-size: 24rpx;
		background: #33C2B4;
		border-radius: 10rpx;
	}
	.scroll-Y {
		height: 300rpx;
	}

	.speSel_item {
		margin-bottom: 8rpx;

		&:last-child {
			margin-bottom: 0;
		}

		h5 {
			font-size: 28rpx;
			margin-bottom: 32rpx;
		}

		.speSel_item_view {
			display: flex;
			align-items: center;
			flex-wrap: wrap;

			view {
				padding: 18rpx 32rpx;
				font-size: 24rpx;
				margin: 0 32rpx 32rpx 0;
				color: #1b2120;
				background: #ffffff;
				border-radius: 6rpx;
				border: 2rpx solid #cacccc;
				position: relative;

				text {
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 24rpx;
					width: 56rpx;
					height: 32rpx;
					color: #ffffff;
					border-radius: 3px 3px 0px 3px;
					position: absolute;
					right: 0;
					top: -16rpx;
				}
			}

			// 缺货
			.outOfStock {
				color: #cacccc;
				border: 1px solid #f8f8f8;
				text {
					color: #ffffff;
					background: #cacccc;
				}
			}

			.outOfStockText {
				background: #cacccc;
			}

			//选中
			.active {
				color: #33C2B4;
				border: 2rpx solid #33C2B4;
				background: #f3fff5;

				text {
					background: #33C2B4;
					right: -2rpx;
				}
			}
		}
	}
}

// 购买数量
.goodsaleinformation {
	margin-top: 40rpx;
	display: flex;
	justify-content: space-between;
	color: #333;
	font-size: 28rpx;

	.keepyears {
		font-weight: 700;
		font-size: 28rpx;
		display: flex;
		align-items: center;

		view {
			color: #aaaaaa;
			font-weight: 100;
			font-size: 24upx;
			margin-left: 16upx;
		}
	}
}

// 已购买数量
.purchaseLimitType {
	width: 100%;
	color: #aaaaaa;
	display: flex;
	font-size: 24rpx;
	justify-content: flex-end;
}
.onePurchaseLimitType {
	width: 616rpx;
	margin-top: 20rpx;
}
.button {
	width: 100%;
	height: 88rpx;
	color: #ffffff;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 60rpx auto 0;
	background: #33C2B4;
	font-size: 30rpx;
	border-radius: 12rpx;
}
.buttonTwo {
	width: 100%;
	height: 88rpx;
	font-size: 30rpx;
	margin: 60rpx auto 0;
	view {
		width: 288rpx;
		height: 100%;
		color: #fff;
		background: #33C2B4;
		border: 1px solid #33C2B4;
		border-radius: 44rpx;
		&:first-child {
			color: #33C2B4;
			background-color: #fff;
		}
		&.buttonTwo_qone {
			width: 100%;
			color: #fff;
			background: #33C2B4;
		}
	}
}
//刻字  预售  结束
.ge {
	width: 100%;
	height: 2rpx;
	background: #f5f5f5;
}

.Color333 {
	color: #1b2120;
}

/deep/uni-checkbox .uni-checkbox-input {
	border: 1px solid #cacccc;
	background: #ffffff;
	border-radius: 50%;
	margin: 0;
}

/deep/uni-checkbox .uni-checkbox-input-checked {
	border: 1px solid #33C2B4;
	background: #33C2B4;
}

/deep/uni-checkbox .uni-checkbox-input:before {
	color: #ffffff;
	// #ifdef H5
	left: 32%;
	// #endif
}
.tip-box {
	display: flex;
	align-items: center;
	justify-content: center;
	position: fixed;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
	z-index: 1001;
	background: rgba(0, 0, 0, 0.6);
	line-height: normal;

	.box {
		width: 560upx;
		margin: auto;
		background: #fff;
		border-radius: 36upx;

		.light {
			display: block;
			width: 520upx;
			height: 192upx;
			margin: -192upx auto 0;
		}

		.cry {
			display: block;
			width: 128upx;
			height: 128upx;
			margin: -64upx auto 0;

			&.left {
				margin: initial;
			}
		}
	}
}
.costPrice {
	color: #aaaaaa;
	font-weight: 400;
	text-decoration: line-through;
}
</style>
